package edu.mse.camel.client.uml.collab.outlineview;

import org.eclipse.gef.GraphicalViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.IContributedContentsView;
import org.eclipse.ui.part.IPage;
import org.eclipse.ui.part.IPageBookViewPage;
import org.eclipse.ui.part.MessagePage;
import org.eclipse.ui.part.PageBook;
import org.eclipse.ui.part.PageBookView;

import edu.mse.camel.client.events.server.QueryLoginStatus;



/**
 * @author darpan
 *
 */
public class PosterBoardOutlineView extends PageBookView implements ISelectionProvider,
ISelectionChangedListener {

	/**
	 * Message to show on the default page.
	 */
	private String defaultText ="An overview is not available."; 

	/**
	 * Creates a content outline view with no content outline pages.
	 */
	public PosterBoardOutlineView() {
		super();
	}

	/* (non-Javadoc)
	 * Method declared on ISelectionProvider.
	 */
	/**
	 * 
	 * 
	 * @param listener 
	 */
	public void addSelectionChangedListener(ISelectionChangedListener listener) {
		getSelectionProvider().addSelectionChangedListener(listener);
	}

	/* (non-Javadoc)
	 * Method declared on PageBookView.
	 */
	/**
	 * 
	 * 
	 * @param book 
	 * 
	 * @return 
	 */
	@Override
	protected IPage createDefaultPage(PageBook book) {
		MessagePage page = new MessagePage();
		initPage(page);
		page.createControl(book);
		page.setMessage(defaultText);
		return page;
	}

	/* (non-Javadoc)
	 * Method declared on PageBookView.
	 */
	@Override
	protected PageRec doCreatePage(IWorkbenchPart part) {
		Object obj = createPageForEditor(part);
		if( obj == null) {
			return null;
		}
		IPage page = (IPage) obj;
		if (page instanceof IPageBookViewPage) {
			initPage((IPageBookViewPage) page);
		}
		page.createControl(getPageBook());
		return new PageRec(part, page);
	}

	/**
	 * Create a new page for the specified Editor.
	 * 
	 * @param part 
	 * 
	 * @return 
	 */
	private Object createPageForEditor(IWorkbenchPart part) {
		GraphicalViewer viewer = (GraphicalViewer) part.getAdapter(GraphicalViewer.class);
		if(viewer == null) {
			return null;
		}
		return new PosterBoardThumbnailPage(viewer);
	}

	/* (non-Javadoc)
	 * Method declared on PageBookView.
	 */
	/**
	 * 
	 * 
	 * @param part 
	 * @param rec 
	 */
	@Override
	protected void doDestroyPage(IWorkbenchPart part, PageRec rec) {
		IPage page = rec.page;
		page.dispose();
		rec.dispose();
	}

	/* (non-Javadoc)
	 * Method declared on IAdaptable.
	 */
	/**
	 * 
	 * 
	 * @param key 
	 * 
	 * @return 
	 */
	@Override
	public Object getAdapter(Class key) {
		if (key == IContributedContentsView.class) {
			return new IContributedContentsView() {
				public IWorkbenchPart getContributingPart() {
					return getContributingEditor();
				}
			};
		}
		return super.getAdapter(key);
	}

	/* (non-Javadoc)
	 * Method declared on PageBookView.
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	protected IWorkbenchPart getBootstrapPart() {
		IWorkbenchPage page = getSite().getPage();
		if (page != null) {
			return page.getActiveEditor();
		}

		return null;
	}

	/**
	 * Returns the editor which contributed the current
	 * page to this view.
	 * 
	 * @return the editor which contributed the current page
	 * or <code>null</code> if no editor contributed the current page
	 */
	private IWorkbenchPart getContributingEditor() {
		return getCurrentContributingPart();
	}

	/* (non-Javadoc)
	 * Method declared on ISelectionProvider.
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	public ISelection getSelection() {
		// get the selection from the selection provider
		return getSelectionProvider().getSelection();
	}

	/* (non-Javadoc)
	 * Method declared on PageBookView.
	 * We only want to track editors.
	 */
	/**
	 * 
	 * 
	 * @param part 
	 * 
	 * @return 
	 */
	@Override
	protected boolean isImportant(IWorkbenchPart part) {
		//We only care about editors
		return (part instanceof IEditorPart);
	}

	/* (non-Javadoc)
	 * Method declared on IViewPart.
	 * Treat this the same as part activation.
	 */
	/**
	 * 
	 * 
	 * @param part 
	 */
	@Override
	public void partBroughtToTop(IWorkbenchPart part) {
		partActivated(part);
	}

	/* (non-Javadoc)
	 * Method declared on ISelectionProvider.
	 */
	/**
	 * 
	 * 
	 * @param listener 
	 */
	public void removeSelectionChangedListener(
			ISelectionChangedListener listener) {
		getSelectionProvider().removeSelectionChangedListener(listener);
	}

	/* (non-Javadoc)
	 * Method declared on ISelectionChangedListener.
	 */
	/**
	 * 
	 * 
	 * @param event 
	 */
	public void selectionChanged(SelectionChangedEvent event) {
		getSelectionProvider().selectionChanged(event);
	}

	/* (non-Javadoc)
	 * Method declared on ISelectionProvider.
	 */
	/**
	 * 
	 * 
	 * @param selection 
	 */
	public void setSelection(ISelection selection) {
		getSelectionProvider().setSelection(selection);
	}
	
}